﻿using System;
using RuntimeGizmos;
using UnityEngine;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000268 RID: 616
	public class SkeletonEditHandle : MonoBehaviour
	{
		// Token: 0x17000195 RID: 405
		// (get) Token: 0x06000DA3 RID: 3491 RVA: 0x0006B1BE File Offset: 0x000693BE
		// (set) Token: 0x06000DA4 RID: 3492 RVA: 0x0006B1C6 File Offset: 0x000693C6
		public bool Selected
		{
			get
			{
				return this._Selected;
			}
			set
			{
				this._Selected = value;
				this.SetStyle(this.style);
			}
		}

		// Token: 0x17000196 RID: 406
		// (get) Token: 0x06000DA5 RID: 3493 RVA: 0x0006B1DB File Offset: 0x000693DB
		// (set) Token: 0x06000DA6 RID: 3494 RVA: 0x0006B1E3 File Offset: 0x000693E3
		public bool ShowPaired
		{
			get
			{
				return this._ShowPaired;
			}
			set
			{
				this._ShowPaired = value;
				this.SetStyle(this.style);
			}
		}

		// Token: 0x17000197 RID: 407
		// (get) Token: 0x06000DA7 RID: 3495 RVA: 0x0006B1F8 File Offset: 0x000693F8
		// (set) Token: 0x06000DA8 RID: 3496 RVA: 0x0006B200 File Offset: 0x00069400
		public bool ShowLinked
		{
			get
			{
				return this._ShowLinked;
			}
			set
			{
				this._ShowLinked = value;
				this.SetStyle(this.style);
			}
		}

		// Token: 0x17000198 RID: 408
		// (get) Token: 0x06000DA9 RID: 3497 RVA: 0x0006B215 File Offset: 0x00069415
		// (set) Token: 0x06000DAA RID: 3498 RVA: 0x0006B21D File Offset: 0x0006941D
		public EditBone EditBone { get; private set; }

		// Token: 0x17000199 RID: 409
		// (get) Token: 0x06000DAB RID: 3499 RVA: 0x0006B226 File Offset: 0x00069426
		private Quaternion boneRotation
		{
			get
			{
				return this.EditBone.RealTransform.rotation;
			}
		}

		// Token: 0x06000DAC RID: 3500 RVA: 0x0006B238 File Offset: 0x00069438
		public void AssignBone(EditBone bone, EntityBase target)
		{
			this.target = target;
			this.EditBone = bone;
			base.transform.SetParent(target.transform);
			this.scale = 0.025f;
			if (target.isGiantess)
			{
				this.scale *= 1000f;
			}
			this.UpdatePosition(TransformType.Move);
		}

		// Token: 0x06000DAD RID: 3501 RVA: 0x0006B294 File Offset: 0x00069494
		private void LateUpdate()
		{
			float num = this.scale * this.scaleMult;
			if (this.Selected)
			{
				num *= 1.5f;
			}
			Vector3 localScale;
			localScale.x = num / base.transform.localScale.x;
			localScale.y = num / base.transform.localScale.y;
			localScale.z = num / base.transform.localScale.z;
			this.myRenderer.transform.localScale = localScale;
		}

		// Token: 0x06000DAE RID: 3502 RVA: 0x0006B328 File Offset: 0x00069528
		private void OnDisable()
		{
			this._ShowPaired = false;
			this._ShowLinked = false;
			this.SetStyle(this.style);
		}

		// Token: 0x06000DAF RID: 3503 RVA: 0x0006B344 File Offset: 0x00069544
		public void SetStyle(SkeletonEditHandle.HandleStyle style)
		{
			this.style = style;
			if (this.Selected)
			{
				this.myRenderer.material = this.selectedMaterial;
			}
			else if (this.ShowPaired)
			{
				this.myRenderer.material = this.pairedMaterial;
			}
			else if (this.ShowLinked)
			{
				this.myRenderer.material = this.linkedMaterial;
			}
			else if (style == SkeletonEditHandle.HandleStyle.Active)
			{
				this.myRenderer.material = this.activeMaterial;
			}
			else if (style == SkeletonEditHandle.HandleStyle.Inactive)
			{
				this.myRenderer.material = this.inactiveMaterial;
			}
		}

		// Token: 0x06000DB0 RID: 3504 RVA: 0x0006B3EF File Offset: 0x000695EF
		public void SetSize(float size)
		{
			this.scaleMult = size;
			this.UpdateLineRenderer();
		}

		// Token: 0x06000DB1 RID: 3505 RVA: 0x0006B400 File Offset: 0x00069600
		public TransformationData UpdateHandle(string id, TransformType transformationType, SkeletonEditOptions options)
		{
			TransformationData result = default(TransformationData);
			result.rotation = Quaternion.identity;
			if (!this.EditBone)
			{
				return result;
			}
			if (transformationType == TransformType.Move && this.previousLocalPosition != base.transform.localPosition)
			{
				result.movement = this.HandleMovement(id, options.movement);
			}
			else if (transformationType == TransformType.Rotate && this.previousLocalRotation != base.transform.localRotation)
			{
				result.rotation = this.HandleRotation(id, options.rotation);
			}
			else if (transformationType == TransformType.Scale && base.transform.localScale != Vector3.one * this.scale)
			{
				result.scaling = this.HandleScaling(id, options.scaling);
			}
			this.UpdatePosition(transformationType);
			return result;
		}

		// Token: 0x06000DB2 RID: 3506 RVA: 0x0006B4F0 File Offset: 0x000696F0
		public void UpdateHandle(string id, TransformationData data, TransformType type, SkeletonEditOptions options)
		{
			if (!this.EditBone)
			{
				return;
			}
			if (data.movement != Vector3.zero)
			{
				this.HandleMovement(id, data.movement, options.movement);
			}
			if (data.rotation != Quaternion.identity)
			{
				this.HandleRotation(id, data.rotation, options.rotation);
			}
			if (!(data.scaling == Vector3.zero) && !(data.scaling == Vector3.one))
			{
				this.HandleScaling(id, data.scaling, options.scaling);
			}
			this.UpdatePosition(type);
		}

		// Token: 0x06000DB3 RID: 3507 RVA: 0x0006B5AC File Offset: 0x000697AC
		private Vector3 HandleMovement(string id, SkeletonEditMovementOptions movementOptions)
		{
			Vector3 vector = base.transform.localPosition - this.previousLocalPosition;
			this.EditBone.Move(id, vector, movementOptions, true);
			return vector;
		}

		// Token: 0x06000DB4 RID: 3508 RVA: 0x0006B5E1 File Offset: 0x000697E1
		private void HandleMovement(string id, Vector3 movementLine, SkeletonEditMovementOptions movementOptions)
		{
			this.EditBone.Move(id, movementLine, movementOptions, true);
		}

		// Token: 0x06000DB5 RID: 3509 RVA: 0x0006B5F4 File Offset: 0x000697F4
		private Quaternion HandleRotation(string id, SkeletonEditRotationOptions rotationOptions)
		{
			Quaternion quaternion = Quaternion.Inverse(this.boneRotation) * base.transform.rotation;
			this.EditBone.Rotate(id, quaternion, rotationOptions);
			return quaternion;
		}

		// Token: 0x06000DB6 RID: 3510 RVA: 0x0006B62C File Offset: 0x0006982C
		private void HandleRotation(string id, Quaternion rotation, SkeletonEditRotationOptions rotationOptions)
		{
			this.EditBone.Rotate(id, rotation, rotationOptions);
		}

		// Token: 0x06000DB7 RID: 3511 RVA: 0x0006B63C File Offset: 0x0006983C
		private Vector3 HandleScaling(string id, SkeletonEditScalingOptions scalingOptions)
		{
			float num = this.scale;
			if (this.Selected)
			{
				num *= 2f;
			}
			Vector3 vector = new Vector3(base.transform.localScale.x / num, base.transform.localScale.y / num, base.transform.localScale.z / num);
			if (vector == Vector3.one)
			{
				return vector;
			}
			Vector3 result = vector;
			vector = this.CorrectToScale(vector);
			this.EditBone.Scale(id, vector, scalingOptions);
			return result;
		}

		// Token: 0x06000DB8 RID: 3512 RVA: 0x0006B6D7 File Offset: 0x000698D7
		private void HandleScaling(string id, Vector3 scaleChange, SkeletonEditScalingOptions scalingOptions)
		{
			scaleChange = this.CorrectToScale(scaleChange);
			this.EditBone.Scale(id, scaleChange, scalingOptions);
		}

		// Token: 0x06000DB9 RID: 3513 RVA: 0x0006B6F0 File Offset: 0x000698F0
		private Vector3 CorrectToScale(Vector3 scaleVector)
		{
			float num = this.target.Scale;
			scaleVector.x = this.CorrectAxisToScale(scaleVector.x, num);
			scaleVector.y = this.CorrectAxisToScale(scaleVector.y, num);
			scaleVector.z = this.CorrectAxisToScale(scaleVector.z, num);
			return scaleVector;
		}

		// Token: 0x06000DBA RID: 3514 RVA: 0x0006B749 File Offset: 0x00069949
		private float CorrectAxisToScale(float axis, float scale)
		{
			if (axis >= 1f)
			{
				return (axis - 1f) / scale + 1f;
			}
			return 1f - (1f - axis) / scale;
		}

		// Token: 0x06000DBB RID: 3515 RVA: 0x0006B775 File Offset: 0x00069975
		private float MultiplyDifference(float value, float mult)
		{
			if (value > 1f)
			{
				value = (value - 1f) * mult + 1f;
			}
			else
			{
				value = 1f - (1f - value) * mult;
			}
			return value;
		}

		// Token: 0x06000DBC RID: 3516 RVA: 0x0006B7AC File Offset: 0x000699AC
		public void UpdatePosition(TransformType transformationType)
		{
			if (!this.EditBone || !this.EditBone.RealTransform)
			{
				return;
			}
			base.transform.position = this.EditBone.RealTransform.position;
			this.previousLocalPosition = base.transform.localPosition;
			if (transformationType == TransformType.Move)
			{
				base.transform.rotation = this.target.transform.rotation;
			}
			else if (transformationType == TransformType.Rotate)
			{
				base.transform.rotation = this.boneRotation;
			}
			else if (transformationType == TransformType.Scale)
			{
				base.transform.rotation = this.EditBone.RealTransform.rotation;
			}
			this.previousLocalRotation = base.transform.localRotation;
			base.transform.localScale = Vector3.one * this.scale;
			if (this.Selected)
			{
				base.transform.localScale = base.transform.localScale * 2f;
			}
			this.UpdateLineRenderer();
		}

		// Token: 0x06000DBD RID: 3517 RVA: 0x0006B8D0 File Offset: 0x00069AD0
		private void UpdateLineRenderer()
		{
			if (this.EditBone && this.EditBone.ParentBone)
			{
				Vector3[] positions = new Vector3[]
				{
					this.EditBone.RealTransform.position,
					this.EditBone.ParentBone.RealTransform.position
				};
				this.myLineRenderer.enabled = true;
				this.myLineRenderer.SetPositions(positions);
				float num = this.target.Scale * 0.01f * this.scaleMult;
				if (this.target.isGiantess)
				{
					num *= 1000f;
				}
				this.myLineRenderer.startWidth = num;
				this.myLineRenderer.endWidth = num;
			}
			else
			{
				this.myLineRenderer.enabled = false;
			}
		}

		// Token: 0x06000DBE RID: 3518 RVA: 0x0006B9B8 File Offset: 0x00069BB8
		public void Reset(string id, TransformType transformationType, SkeletonEditOptions options)
		{
			if (!this.EditBone)
			{
				return;
			}
			if (transformationType == TransformType.Move)
			{
				this.EditBone.ResetPosition(id, options.movement);
			}
			else if (transformationType == TransformType.Rotate)
			{
				this.EditBone.ResetRotation(id, options.rotation);
			}
			else if (transformationType == TransformType.Scale)
			{
				this.EditBone.ResetScaling(id, options.scaling);
			}
			this.UpdatePosition(transformationType);
		}

		// Token: 0x06000DBF RID: 3519 RVA: 0x0006BA34 File Offset: 0x00069C34
		public void ResetBone(string id, SkeletonEditOptions options)
		{
			if (!this.EditBone)
			{
				return;
			}
			this.EditBone.ResetPosition(id, options.movement);
			this.EditBone.ResetRotation(id, options.rotation);
			this.EditBone.ResetScaling(id, options.scaling);
			this.UpdatePosition(TransformType.Move);
		}

		// Token: 0x06000DC0 RID: 3520 RVA: 0x0006BA8F File Offset: 0x00069C8F
		public void ResetAll(string id)
		{
			if (!this.EditBone)
			{
				return;
			}
			this.EditBone.ResetAll(id);
			this.UpdatePosition(TransformType.Move);
		}

		// Token: 0x06000DC1 RID: 3521 RVA: 0x0006BAB5 File Offset: 0x00069CB5
		public void Disable(Transform folder)
		{
			base.transform.SetParent(folder);
			this.EditBone = null;
			this._ShowLinked = false;
			this._ShowPaired = false;
			this.Selected = false;
			base.gameObject.SetActive(false);
		}

		// Token: 0x04001284 RID: 4740
		private const float GIANTESS_SCALE_MULTIPLIER = 1000f;

		// Token: 0x04001285 RID: 4741
		private const float HANDLE_SIZE = 0.025f;

		// Token: 0x04001286 RID: 4742
		private const float LINE_WIDTH = 0.01f;

		// Token: 0x04001287 RID: 4743
		[SerializeField]
		private Renderer myRenderer;

		// Token: 0x04001288 RID: 4744
		[SerializeField]
		private LineRenderer myLineRenderer;

		// Token: 0x04001289 RID: 4745
		[SerializeField]
		private Material activeMaterial;

		// Token: 0x0400128A RID: 4746
		[SerializeField]
		private Material inactiveMaterial;

		// Token: 0x0400128B RID: 4747
		[SerializeField]
		private Material selectedMaterial;

		// Token: 0x0400128C RID: 4748
		[SerializeField]
		private Material pairedMaterial;

		// Token: 0x0400128D RID: 4749
		[SerializeField]
		private Material linkedMaterial;

		// Token: 0x0400128E RID: 4750
		private bool _Selected;

		// Token: 0x0400128F RID: 4751
		private bool _ShowPaired;

		// Token: 0x04001290 RID: 4752
		private bool _ShowLinked;

		// Token: 0x04001291 RID: 4753
		private SkeletonEditHandle.HandleStyle style;

		// Token: 0x04001292 RID: 4754
		private EntityBase target;

		// Token: 0x04001294 RID: 4756
		private Vector3 previousLocalPosition;

		// Token: 0x04001295 RID: 4757
		private Quaternion previousLocalRotation;

		// Token: 0x04001296 RID: 4758
		private float scale;

		// Token: 0x04001297 RID: 4759
		private float scaleMult = 1f;

		// Token: 0x02000269 RID: 617
		public enum HandleStyle
		{
			// Token: 0x04001299 RID: 4761
			Active,
			// Token: 0x0400129A RID: 4762
			Inactive
		}
	}
}
